cmake_minimum_required(VERSION 3.14.5 FATAL_ERROR)
project(gtjaudpapi LANGUAGES CXX VERSION 3.7.0.20221227)

# 为安装和打包功能设置一些选项，默认只安装动态库文件和可执行文件，不安装静态库文件、头文件或导出库文件，也不打包。
option(${PROJECT_NAME}_INSTALL "启用安装功能，默认只安装动态库文件和可执行文件" ON)
option(${PROJECT_NAME}_INSTALL_ALL "安装所有的头文件、库文件、导出库文件和可执行文件" OFF)
option(${PROJECT_NAME}_ENABLE_CPACK "启用打包功能，对项目最终安装的文件目录打包以便分发、发布" OFF)
set(${PROJECT_NAME}_COPY_TO_PATH "" CACHE PATH "将附加的动态依赖库复制到指定目录，比如Windows下可以复制到输出目录以方便调试。")

# 在这里进行平台的检查控制，并设置各种库的目标和变量等。
if(UNIX)
    list(APPEND import_targets_name udp)
    set(udp_root ${CMAKE_CURRENT_SOURCE_DIR}/)
    set(include_directory_udp ${udp_root}/include)
    set(header_files_udp
            ${include_directory_udp}/GtjaMdUserApi.h
            ${include_directory_udp}/GtjaMdStructV3.h)
    set(import_location_udp ${udp_root}/Linux64/gcc9.3.1/GtjaMdUserApi.so)
elseif(WIN32)
    list(APPEND import_targets_name udp)
    if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "X86")
        set(udp_root ${CMAKE_CURRENT_SOURCE_DIR}/)
        set(include_directory_udp ${udp_root}/include)
        set(header_files_udp
            ${include_directory_udp}/GtjaMdUserApi.h
            ${include_directory_udp}/GtjaMdStructV3.h)
        set(import_location_udp ${udp_root}/Win32/GtjaMdUserApi.dll)
        set(implib_location_udp ${udp_root}/Win32/GtjaMdUserApi.lib)
    elseif(CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "x64")
        set(udp_root ${CMAKE_CURRENT_SOURCE_DIR}/)
        set(include_directory_udp ${udp_root}/include)
        set(header_files_udp
            ${include_directory_udp}/GtjaMdUserApi.h
            ${include_directory_udp}/GtjaMdStructV3.h)
        set(import_location_udp ${udp_root}/Win64/GtjaMdUserApi.dll)
        set(implib_location_udp ${udp_root}/Win64/GtjaMdUserApi.lib)
    else()
        message(FATAL_ERROR "Arch=${CMAKE_CXX_COMPILER_ARCHITECTURE_ID} on ${CMAKE_SYSTEM_NAME} NOT supported.")
    endif()

else()
    message(FATAL_ERROR "Arch=${CMAKE_CXX_COMPILER_ARCHITECTURE_ID} on ${CMAKE_SYSTEM_NAME} NOT supported.")
endif()

# 在这里把必要的依赖项给添加上
find_package(Threads REQUIRED)

# 将API头文件和库文件添加成IMPORTED库目标，以供其他CMake项目引用。
foreach(target_name ${import_targets_name})
    add_library(${PROJECT_NAME}::${target_name} INTERFACE IMPORTED GLOBAL)
    target_include_directories(${PROJECT_NAME}::${target_name}
            INTERFACE $<BUILD_INTERFACE:${include_directory_${target_name}}>
            INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

    get_filename_component(_import_directory_${target_name} ${import_location_${target_name}} DIRECTORY)
    # message(STATUS "_import_directory_${target_name} = ${_import_directory_${target_name}}")
    if(implib_location_${target_name})
        get_filename_component(_implib_directory_${target_name} ${implib_location_${target_name}} DIRECTORY)
        # message(STATUS "_implib_directory_${target_name} = ${_implib_directory_${target_name}}")
    endif()
    target_link_directories(${PROJECT_NAME}::${target_name}
            INTERFACE $<$<PLATFORM_ID:Linux>:${_import_directory_${target_name}}>
            INTERFACE $<$<PLATFORM_ID:Windows>:${_implib_directory_${target_name}}>)

    get_filename_component(_import_filename_${target_name} ${import_location_${target_name}} NAME)
    # message(STATUS "_import_filename_${target_name} = ${_import_filename_${target_name}}")
    if(implib_location_${target_name})
        get_filename_component(_implib_filename_${target_name} ${implib_location_${target_name}} NAME)
        # message(STATUS "_implib_filename_${target_name} = ${_implib_filename_${target_name}}")
    endif()
    target_link_libraries(${PROJECT_NAME}::${target_name}
            INTERFACE $<$<PLATFORM_ID:Linux>::${_import_filename_${target_name}}> # 注意两个冒号，因为库文件不是标准unix命名方式
            INTERFACE $<$<PLATFORM_ID:Windows>:${_implib_filename_${target_name}}>
            INTERFACE Threads::Threads)
endforeach()

# 将动态库文件复制到指定目录
if(${PROJECT_NAME}_COPY_TO_PATH)
    if(NOT EXISTS ${${PROJECT_NAME}_COPY_TO_PATH})
        message(FATAL_ERROR "${PROJECT_NAME}_COPY_TO_PATH NOT EXISTS: ${${PROJECT_NAME}_COPY_TO_PATH}")
    endif()

    # message(STATUS "${PROJECT_NAME}_COPY_TO_PATH = ${${PROJECT_NAME}_COPY_TO_PATH}")
    foreach(target_name ${import_targets_name})
        # message(STATUS "import_location_${target_name} = ${import_location_${target_name}}")
        file(COPY ${import_location_${target_name}} DESTINATION ${${PROJECT_NAME}_COPY_TO_PATH})
    endforeach()
endif()

# 对于安装的文件名，Windows平台下动态库和导出库不要以lib开头，静态库要以lib开头；UNIX平台下动态库和静态库都要以lib开头。
# Windows下动态库安装在${CMAKE_INSTALL_BINDIR}下，其他库安装在${CMAKE_INSTALL_LIBDIR}；Unix下库都安装在${CMAKE_INSTALL_LIBDIR}下。
# 可执行项目，可执行文件安装在${CMAKE_INSTALL_BINDIR}下，配置文件安装在${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME}下；
# 库项目，可执行文件应当安装在${CMAKE_INSTALL_LIBEXECDIR}目录下，配置文件安装在${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}下。
# 特定用户的配置文件和数据文件不应当被安装，应当在程序运行时生成在$HOME/.${PROJECT_NAME}下，或者%USERPROFILE%/.${PROJECT_NAME}下。
if(${PROJECT_NAME}_INSTALL)
    include(GNUInstallDirs)
    if(WIN32)
        set(runtime_install_dir ${CMAKE_INSTALL_BINDIR})
    else()
        set(runtime_install_dir ${CMAKE_INSTALL_LIBDIR})
    endif()

    foreach(target_name ${import_targets_name})
        install(FILES ${import_location_${target_name}} DESTINATION ${runtime_install_dir})
        if(${PROJECT_NAME}_INSTALL_ALL)
            if(WIN32)
                install(FILES ${implib_location_${target_name}} DESTINATION ${CMAKE_INSTALL_LIBDIR})
            endif()
            install(FILES ${header_files_${target_name}} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
        endif()
    endforeach()

    if(${PROJECT_NAME}_ENABLE_CPACK)
        include(CPack)
    endif()
endif()
